package com.wxb.plugin.core.parser;

import com.intellij.psi.*;
import com.wxb.plugin.core.gen.StringUtil;

import java.util.ArrayList;
import java.util.List;

import static com.wxb.plugin.core.gen.QualifyClassName.*;

/**
 * @author wxb
 * @createTime 2025/2/27 16:11
 */
public class URLParser extends Parser {
    static List<String> methodAnn = new ArrayList<String>();

    static {
        methodAnn.add(RequestMapping);
        methodAnn.add(GetMapping);
        methodAnn.add(PostMapping);
        methodAnn.add(PutMapping);
        methodAnn.add(DeleteMapping);
    }

    public static String getClassUrl(PsiClass aClass) {
        return trimUrl(getPath(aClass.getAnnotation(RequestMapping)));
    }

    public static boolean hide(PsiAnnotation annotation) {
        if(annotation == null){
            return false;
        }
        return Hidden.equals(annotation.getQualifiedName());
    }


    public static List<String> getMethodUrl(PsiMethod method, String prefix) {
        String type;
        List<String> paths = new ArrayList<>();
        for (PsiAnnotation methodAnnotation : method.getAnnotations()) {
            if (PostMapping.equals(methodAnnotation.getQualifiedName())) {
                type = "POST";
            } else if (PutMapping.equals(methodAnnotation.getQualifiedName())) {
                type = "PUT";
            } else if (GetMapping.equals(methodAnnotation.getQualifiedName())) {
                type = "GET";
            } else if (DeleteMapping.equals(methodAnnotation.getQualifiedName())) {
                type = "DELETE";
            } else if (RequestMapping.equals(methodAnnotation.getQualifiedName())) {
                type = getMethodType(getText(methodAnnotation, false,"method"));
            } else {
                type = null;
            }
            String text = getText(methodAnnotation, "path", "value", "name");

            getMethodPath(text, prefix, type, paths);
        }
        return paths;
    }

    public static void getMethodPath( String text, String prefix, String type, List<String> paths) {
        if(type == null){
            return;
        }
        if (StringUtil.isNotBlank(text)) {
            for (String pathValue : text.replaceAll("\"","").split(",")) {
                paths.add(type + ": " + prefix + trimUrl(pathValue));
            }
        } else {
            paths.add(type + ": " + prefix);
        }
    }

    public static String getMethodType(String methods) {
        if(StringUtil.isNotBlank(methods)){
            return methods.replaceAll(",","、")
                    .replaceAll("RequestMethod.","")
                    .replaceAll("\\{","")
                    .replaceAll("}", "");
        }else {
            return methods;
        }
    }

    public static String trimUrl(String url) {
        url = url.trim().startsWith("/") ? url : "/" + url;
        return url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
    }

    public static String getPath(PsiAnnotation annotation) {
        if (annotation == null) {
            return "";
        }
        String s;
        if (StringUtil.isNotBlank(s = getText(annotation, "path"))) {
            return s;
        }
        if (StringUtil.isNotBlank(s = getText(annotation, "value"))) {
            return s;
        }
        if (StringUtil.isNotBlank(s = getText(annotation, "name"))) {
            return s;
        }
        return "";
    }
}
